<?php

namespace app\modules\admin\controllers;

use app\common\services\StaticServices;
use app\common\services\UploadServices;
use app\models\Admin;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Response;
use Yii;
use yii\web\UploadedFile;

/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller {
    public $enableCsrfValidation = false;

    /**
     * 用户登录控制
     */
    public function actionLogin()
    {
        $this->layout = false;
        if (Yii::$app->request->isPost) {
            try {
                Yii::$app->response->format = Response::FORMAT_JSON;
                $admin_name = Yii::$app->request->post('admin_name', '');
                $admin_pwd = Yii::$app->request->post('admin_pwd', '');
                if (empty($admin_name) || empty($admin_pwd)) {
                    throw new Exception('用户名或密码不能空');
                }
                $check = Admin::find()->where(['admin_name' => $admin_name])->one();

                if ($check == false) {
                    throw new Exception('用户不存在');
                }
                if (md5($admin_pwd) !== $check->admin_pwd) {
                    throw new Exception('用户名密码不正确');
                }
                Yii::$app->admin->login($check);
                return ['code' => 200, 'admin_id' => Yii::$app->admin->identity->id];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }

        return $this->render('login');
    }

    public function actionTest()
    {
        $result = StaticServices::getUserRole();
        unset($result[4]);
        var_dump($result);
        die;
    }
}
